package Hxw3DEngine.core.materials.shader.filters {

	import flash.geom.Matrix3D;
	import flash.geom.Orientation3D;
	import flash.geom.Vector3D;
	
	import Hxw3DEngine.core.Resource.geometry.ResourceGeometry;
	import Hxw3DEngine.core.materials.shader.utils.FcRegisterLabel;
	import Hxw3DEngine.core.materials.shader.utils.ShaderRegisterCache;
	import Hxw3DEngine.core.materials.shader.utils.ShaderRegisterElement;
	import Hxw3DEngine.core.materials.shader.utils.VcRegisterLabel;
	
	import Hxw3DEngine.proxy.Stage3DProxy;

	/**
	 * 广告牌
	 * @author Neil
	 *
	 */
	public class BillboardFilter extends Filter3D {

		public static const RADIANS_TO_DEGREES : Number = 180 / Math.PI;

		private var matrix : Matrix3D = new Matrix3D();
		
		public function BillboardFilter() {
			super("BillboardFilter");
			this.priority = 10;
		}
		
		override public function update():void {
			this.matrix.copyFrom(Stage3DProxy.world);
			this.matrix.append(Stage3DProxy.view);
			var comps : Vector.<Vector3D> = matrix.decompose(Orientation3D.AXIS_ANGLE);
			this.matrix.identity();
			this.matrix.appendRotation(-comps[1].w * RADIANS_TO_DEGREES, comps[1]);
		}
		
		override public function getVertexCode(regCache : ShaderRegisterCache) : String {
			super.getVertexCode(regCache);
			var rotate : ShaderRegisterElement = regCache.getVc(4, new VcRegisterLabel(matrix),"billboardMat");
			
			m33(regCache.op.xyz,regCache.getVa(ResourceGeometry.POSITION),rotate);
			
			return code;
		}

	}
}
